package com.sjq.supplier.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sjq.supplier.entity.Type;
import com.sjq.supplier.util.Result;
import lombok.extern.slf4j.Slf4j;
import com.sjq.supplier.entity.Warehouse;
import com.sjq.supplier.service.WarehouseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * (Warehouse)表控制层
 *
 * @author makejava
 * @since 2024-01-02 14:54:53
 */
@Slf4j
@RestController
@RequestMapping("warehouse")
public class WarehouseController {
    /**
     * 服务对象
     */
    @Resource
    private WarehouseService warehouseService;

    /**
     * 分页查询所有数据
     *
     * @param warehouse 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public Result selectAll(Warehouse warehouse,Integer limit, Integer page) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        LambdaQueryWrapper<Warehouse> q = new LambdaQueryWrapper<>();
        q.like(StringUtils.hasLength(warehouse.getName()), Warehouse::getName, warehouse.getName());
        q.like(StringUtils.hasLength(warehouse.getAddress()), Warehouse::getAddress, warehouse.getAddress());
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", warehouseService.list(q).size());
        //分页
        q.last("limit " + (page - 1) * limit + "," + limit);
        result.put("data", warehouseService.list(q));
        return Result.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("findAll")
    public Result findAll() {
        return Result.success(warehouseService.list());
    }

    /**
     * 新增数据
     *
     * @param warehouse 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @Transactional(rollbackFor = Exception.class)
    public Result insert(Warehouse warehouse) {
        //判断warehouse是否为空
        if (warehouse== null) {
            return Result.failure("数据不能为空");
        }

        //判断仓库名称是否存在
        if (warehouseService.count(new LambdaQueryWrapper<Warehouse>().eq(Warehouse::getName, warehouse.getName())) > 0) {
            return Result.failure("仓库名称已存在");
        }

        warehouse.setIsvalid(1);
        if (warehouseService.save(warehouse)) {
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    /**
     * 修改数据
     *
     * @param warehouse 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    @Transactional(rollbackFor = Exception.class)
    public Result update(Warehouse warehouse) {
        //判断warehouse是否为空
        if (warehouse== null) {
            return Result.failure("数据不能为空");
        }

        //判断仓库名称是否存在
        Warehouse one = warehouseService.getOne(new LambdaQueryWrapper<Warehouse>().eq(Warehouse::getName, warehouse.getName()));
        if (one!= null &&!one.getId().equals(warehouse.getId())) {
            return Result.failure("仓库名称已存在");
        }

        if (warehouseService.updateById(warehouse)) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @PostMapping("delete")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> delete(Integer id) {
        //判断id是否为空
        if (id == null) {
            return Result.failure("id不能为空");
        }

        Warehouse warehouse = new Warehouse();
        warehouse.setIsvalid(2);
        warehouse.setId(id);

        if (warehouseService.removeById(warehouse)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }
}

